import { useState, useCallback } from 'react'
import { lists as getLists } from '@/services/blog/article';
import { NORMAL_CODE } from '@/constants';

export default () => {
  const [total, setTotal] = useState(0);
  const [list, setList] = useState([]);
  const [data, setData] = useState({});

  // 获取列表
  const getList = useCallback(async (search) => {
    const res = await getLists(search);
    if (res.code !== NORMAL_CODE) {
      setTotal(0);
      setList([]);
    }else{
      setTotal(res.data.total);
      setList(res.data.list);
    }
  }, [])

  // 获取详情
  const getDetail = useCallback(async (search) => {
    const res = await readDetail(search);
    if (res.code !== NORMAL_CODE) {
      setData({});
    }else{
      setData(res.data);
    }
  }, [])

  return {
    total,
    list,
    getList,
    data,
    getDetail,
  }
}